package com.dragon.jdbc;

import java.sql.*;
import java.util.Properties;

/**
 * 执行sql 平台
 *
 * @author dragon
 * @since 2019/9/25 10:06
 */
public class StatementDemo {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/mybatis_test";
            Driver driver = DriverManager.getDriver(url);
            Properties properties = new Properties();
            properties.setProperty("user", "root");
            properties.setProperty("password", "dragon4369");
            connection = driver.connect(url, properties);
            statement = connection.createStatement();
            System.out.println(statement);
            resultSet = statement.executeQuery("select * from user");
            while (resultSet.next()) {
                System.out.println("id:" + resultSet.getInt("id") + " name:" +
                        resultSet.getString("name"));
            }
        } finally {
            if (connection != null) {
                connection.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (resultSet != null) {
                resultSet.close();
            }
        }
    }
}
